# Maintainer: Peter Budai <peterbud@hotmail.com>

_realname=openlibm
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.7.5
pkgrel=1
pkgdesc="High quality system independent, portable, open source libm implementation (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://openlibm.org'
license=('MIT')
options=('strip' 'staticlibs')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/JuliaMath/${_realname}/archive/v${pkgver}.tar.gz"
        01-mingw-build.patch)
sha256sums=('be983b9e1e40e696e8bbb7eb8f6376d3ca0ae675ae6d82936540385b0eeec15b'
            '8fc88f5296a5963dc0389da656f299d7ace0a26a79882acc2596b686937a6e70')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  apply_patch_with_msg 01-mingw-build.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -rf "${srcdir}/${_realname}-${pkgver}" "${srcdir}/build-${CARCH}"

  cd "${srcdir}/build-${CARCH}"
  make prefix=${MINGW_PREFIX} USEGCC=1
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make check || true
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make  prefix=${MINGW_PREFIX}  DESTDIR="${pkgdir}" install

  install -Dm644 ${srcdir}/build-${CARCH}/LICENSE.md ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
